﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using SmartProject.Entity;
using SmartProject.Data;

using NUnit.Framework;

namespace SmartProject.Testing
{
    [TestFixture]
    public class Test08_RoleAndPermissions
    {
        [Test]
        public void Test01_AddPermissions()
        {
            using (var session = DataProvider.Instance.Session)
            {
                using (var transaction = session.BeginTransaction())
                {
                    Assert.IsNull(Permission.GetPermission(session, "Something"));

                    Permission addProduct = new Permission();
                    addProduct.Action = "AddProduct";
                    addProduct.Save(session);

                    Permission removeProduct = new Permission();
                    removeProduct.Action = "RemoveProduct";
                    removeProduct.Save(session);

                    transaction.Commit();
                    transaction.Dispose();

                    Assert.IsNotNull(Permission.GetPermission(session, "AddProduct"));
                    Assert.IsNotNull(Permission.GetPermission(session, "RemoveProduct"));
                }
            }
        }

        [Test]
        public void Test02_DeletePermissions()
        {
            using (var session = DataProvider.Instance.Session)
            {
                using (var transaction = session.BeginTransaction())
                {
                    Permission addProduct = Permission.GetPermission(session, "AddProduct");
                    addProduct.Delete(session);

                    Permission removeProduct = Permission.GetPermission(session, "RemoveProduct");
                    removeProduct.Delete(session);

                    transaction.Commit();
                    transaction.Dispose();
                }
                Assert.IsNull(Permission.GetPermission(session, "AddProduct"));
                Assert.IsNull(Permission.GetPermission(session, "RemoveProduct"));
                session.Close();
                session.Dispose();
            }
        }

        [Test]
        public void Test03_AddRolePermissions()
        {
            using (var session = DataProvider.Instance.Session)
            {
                Assert.IsNull(Role.GetRole(session, "My New Role"));

                using (var transaction = session.BeginTransaction())
                {
                    for (int i = 0; i < 5; i++)
                    {
                        Permission permission = new Permission();
                        permission.Action = "Action" + i.ToString();
                        permission.Save(session);
                    }

                    Role role = new Role();
                    role.Name = "My New Role";
                    for (int i = 0; i < 5; i++)
                    {
                        role.Permissions.Add(Permission.GetPermission(session, "Action" + i.ToString()));
                    }
                    role.Save(session);

                    Role exitingRole = Role.GetRole(session, "My New Role");
                    Assert.IsNotNull(exitingRole);
                    Assert.IsFalse(exitingRole.HasPermission(session, "Something"));
                    Assert.IsTrue(exitingRole.HasPermission(session, "Action0"));

                    for (int i = 0; i < 5; i++)
                    {
                        Permission permission = Permission.GetPermission(session, "Action" + i.ToString());
                        exitingRole.Permissions.Remove(permission);
                        exitingRole.Save(session);
                        permission.Delete(session);
                    }

                    exitingRole.Delete(session);
                    Assert.IsNull(Role.GetRole(session, "My New Role"));
                    transaction.Commit();
                }
            }
        }
    }
}
